class Solution {
public:
    void dfs(vector<vector<char>>& grid,int x,int y)
    {   
        grid[x][y] = 2;
        if(x-1 >=0 && grid[x-1][y] == '1')dfs(grid,x-1,y);
        if(x+1 <grid.size() &&grid[x+1][y] == '1')dfs(grid,x+1,y);
        if(y-1 >=0 &&grid[x][y-1] == '1')dfs(grid,x,y-1);
        if(y+1 <grid[0].size() &&grid[x][y+1] == '1')dfs(grid,x,y+1);
    }
    int numIslands(vector<vector<char>>& grid) {
        //来一个bfs吧
        int nr = grid.size();
        if(!nr)return 0;
        int nc = grid[0].size();
        int numIsland = 0;
        for(int i = 0 ; i < nr ; ++i)
        {
            for(int j = 0 ; j < nc ; ++j)
            {
                if(grid[i][j] == '1')
                {
                    numIsland++;
                    dfs(grid,i,j);
                }
            }
        }
        return numIsland;
    }
};
